function InsertQuestionImg(questImgLink){	
	///create/{questID}/{answerID}/{questImgLink}
	var QuestionID=gup("QuestionID");	
	var AnswerID=gup("AnswerID");
	if (AnswerID==''){
		AnswerID='0';}
	else
		{
		QuestionID='0';
		}
	if (QuestionID=='')
		QuestionID='0';
	
	var urlPath = "./rest/questionImage/create/"+QuestionID+"/"+AnswerID+"/"+questImgLink+"/";
	//alert(urlPath);
	objPost.onreadystatechange = function(){
		if (objPost.readyState == 4) {
		        if (objPost.status == 200) {
		        	if (window.confirm("Successfull! Would you like to add more figure?")){
		        		location.reload(); 
		        	}else
		        		history.back();
		        }
		        else{
		        	alert( "false");
		        }	
		   }
	};		
	objPost.open("GET",urlPath,true);
	objPost.send(null);	
}

function UpdateQuestionImg(questImgLink){	
	///update/{questImgID}/{questID}/{answerID}/{questImgLink}
	var questImgID=gup("ImgID");
	var QuestionID=gup("QuestionID");
	var AnswerID=gup("AnswerID");	
	if (AnswerID==''){
		AnswerID='0';}
	else
		{
		QuestionID='0';
		}
	if (QuestionID=='')
		QuestionID='0';
	var urlPath = "./rest/questionImage/update/"+questImgID+"/"+QuestionID+"/"+AnswerID+"/"+questImgLink+"/";
	//alert(urlPath);
	objPost.onreadystatechange = function(){
		if (objPost.readyState == 4) {
			 if (objPost.status == 200) {
				 alert("Successfull!");
				 history.back();        	
		        }
		        else{
		        	alert( "false");
		        }	
		   }
	};		
	objPost.open("GET",urlPath,true);
	objPost.send(null);	
}
function DeleteQuestionImg(questImgID){
	if (window.confirm("Are you sure to delete Question - Image relationship have ID: "+questImgID+" ?")){
	var urlPath = "./rest/questionImage/delete/"+questImgID+"/";
	obj.onreadystatechange = function(){		
		if (obj.readyState == 4) {
			//alert(urlPath);
		        if (obj.status == 200) {
		        	location.reload(); 
		        }
		        else{alert("Error: Pls try again!"+obj.responseText+" --- ");}	
		   }
		
	};		
	obj.open("GET",urlPath,true);
	obj.send(null);}
	else
		return;
}	

function InsertByUploadImg(){
	var fd = new FormData();
	var input= document.getElementById("file");
	if (input.value== ""||input.value==null)
		{
			alert("Please select a image");
			return;		
		}		
	fd.append( 'file', input.files[0] );
	var urlPath = "./rest/questionImage/upload/";	
	objPost.onreadystatechange = function(){
		if (objPost.readyState == 4) {
		        if (objPost.status == 200) {
		        	var ImgID=gup("ImgID");
		        	if (ImgID |= "")
		        		UpdateQuestionImg(objPost.responseText);	
		        	else
		        		InsertQuestionImg(objPost.responseText);		        	
		        }
		        else{
		        	alert("Fail");
		        }	
		   }
	};			
	objPost.open('POST', urlPath, true);
	objPost.send(fd);
}